//201902104030_何琛选
package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.ActorDao;
import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.dao.UserDao;
import cn.edu.sdjzu.xg.bysj.domain.Student;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import cn.edu.sdjzu.xg.bysj.domain.User;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
	private static TeacherDao teacherDao= TeacherDao.getInstance();
	private static TeacherService teacherService=new TeacherService();
	private TeacherService(){}

	public static TeacherService getInstance(){
		return teacherService;
	}

	public Collection<Teacher> findAll(
			List<Condition> conditionList_json_str, Pagination pagination)
			throws SQLException {
		//获得链接以便完成可能的事务操作
		Connection connection = JdbcHelper.getConn();
		Collection<Teacher> teachers =
				teacherDao.findAll(connection,conditionList_json_str,pagination);
		JdbcHelper.close(connection);
		return teachers;
	}

	public Teacher find(Integer id) throws SQLException {
		Connection conn = JdbcHelper.getConn();
		Teacher teacher = teacherDao.find(id,conn);
		JdbcHelper.close(conn);
		return teacher;
	}

	public boolean update(Teacher teacherToUpdate) throws SQLException,bysjException {
		Connection connection = JdbcHelper.getConn();
		connection.setAutoCommit(false);
		boolean updated = false;
		try{
			teacherDao.update(teacherToUpdate,connection);
			User user = UserDao.getInstance().find(teacherToUpdate.getId(),connection);
			updated = UserDao.getInstance().update(user,connection);

			//提交事务
			connection.commit();
		}catch (SQLException e){
			//回滚
			connection.rollback();
			throw new bysjException("修改失败");
		}finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null,connection);
		}
		return updated;
	}

	public Boolean add(Teacher teacher) throws SQLException ,bysjException{
		//获得进行事务的连接，添加教师和添加用户都使用本连接对象
		Connection connection = JdbcHelper.getConn();
		//将自动提交设为false，开始事务
		connection.setAutoCommit(false);
		boolean added = false;
		try {
			//添加actor对象，返回值为自动生成的id
			int actorId = ActorDao.getInstance().add(connection);
			teacher.setId(actorId);
			teacherDao.add(teacher, connection);
			//创建用户对象，密码和用户名取自teacher字段值no
			User user = new User(teacher.getNo(), teacher.getNo(), null, teacher);
			//增加用户
			added = UserDao.getInstance().add(user, connection);
			//提交事务
			connection.commit();
		} catch (SQLException e) {
			//回滚事务中所有操作
			connection.rollback();
			throw new bysjException("失败");
		} finally {
			//将自动提交设为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null, connection);
		}
		return added;
	}

	public boolean delete(Integer id) throws SQLException,bysjException{
		Connection connection = JdbcHelper.getConn();
		boolean result = false;
		Teacher teacher = this.find(id);
		result = teacherDao.delete(teacher,connection);
		return result;
	}

	public boolean delete(Teacher teacher) throws SQLException,bysjException{
		Connection connection = JdbcHelper.getConn();
		connection.setAutoCommit(false);
		boolean deleted = false;
		try {
			teacherDao.delete(teacher,connection);
			User user = UserDao.getInstance().find(teacher.getId(),connection);
			deleted = UserDao.getInstance().delete(user,connection);

			//提交事务
			connection.commit();

		} catch (SQLException e){
			//回滚
			connection.rollback();
			throw new bysjException("删除失败");
		}finally {
			//将自动提交设置为true，结束事务
			connection.setAutoCommit(true);
			JdbcHelper.close(null,connection);
		}
		return deleted;
	}
}
